﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace RSIS.Editor
{
   
    public partial class HousebatchControl : UserControl
    {
        private string m_strHouseId;
        private string m_strHouseBatchName;
        private FormType m_FormType;
        private System.Data.DataTable m_pHouseMainTypeTable;
        private Npgsql.NpgsqlDataAdapter m_pHouseMainTypeDataAdapter;
        

        public HousebatchControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 楼盘编号
        /// </summary>
        public string HouseId
        {
            set
            {
                m_strHouseId = value;
            }

         }

        /// <summary>
        /// 获得楼盘期/批次
        /// </summary>
        public string HouseBatchName
        {
          
            set
            {
                m_strHouseBatchName = value;
            }
               
        }

        public FormType FormType
        {
            set
            {
                m_FormType=value;
            }
        }


        public bool Save()
        {
            RSIS.DataBase.RealInfo pRealInfo = new RSIS.DataBase.RealInfo();

            //必须填写楼盘名称
            if (this.txtHouseBatch.Text.Length == 0)
            {
                MessageBox.Show(@"必须填写期\批次!");
                return false;
            }


            Npgsql.NpgsqlConnection pCon = new Npgsql.NpgsqlConnection(pRealInfo.GetADOConStr());
            string strQueryString = "Select * from rsis_housebatch where houseid='"+m_strHouseId+"'";
            Npgsql.NpgsqlDataAdapter pDataAdp = new Npgsql.NpgsqlDataAdapter();
            pDataAdp.SelectCommand = new Npgsql.NpgsqlCommand(strQueryString, pCon);
            Npgsql.NpgsqlCommandBuilder pComBuilder = new Npgsql.NpgsqlCommandBuilder(pDataAdp);


            System.Data.DataSet pDataSet = new DataSet();
            pDataAdp.Fill(pDataSet, "rsis_housebatch");


            System.Data.DataRow pRow;
            if (this.m_FormType == FormType.NEW)
            {
                pRow = pDataSet.Tables["rsis_housebatch"].NewRow();
            }
            else
            {
                
                pRow=pDataSet.Tables["rsis_housebatch"].Select("batchname='" + m_strHouseBatchName + "'")[0];
               
               //找到当前记录                      
           }

            pRow["houseid"]=this.m_strHouseId;
            pRow["batchname"]=this.txtHouseBatch.Text;
            pRow["floors"]=this.txtFLOORS.Text;
            pRow["elevator"]=this.txtELEVATOR.Text;
            pRow["payment"]=this.txtPAYMENT.Text;
            pRow["housetyperange"]=this.txtHOUSETYPERANGE.Text;
            pRow["housetypetrait"]=this.txtHOUSETYPETRAIT.Text;
            pRow["meo"] = this.txtMeo.Text;


            try
            {
                pRow["buildingarea"] = Double.Parse(this.txtBuildingarea.Text);
            }
            catch (FormatException exp)
            {
                if (this.txtBuildingarea.Text != "")
                {
                    MessageBox.Show("建筑面积应为数值类型！", "楼盘信息编辑");
                    return false;
                }
            }

            try
            {
                pRow["combuildingarea"] = Double.Parse(this.txtCOMBUILDINGAREA.Text);
            }
            catch (FormatException exp)
            {
                if (this.txtCOMBUILDINGAREA.Text != "")
                {
                    MessageBox.Show("商业建筑面积应为数值类型！", "楼盘信息编辑");
                    return false;
                }
            }

            try
            {
                pRow["resbuildingarea"] = Double.Parse(this.txtRESBUILDINGAREA.Text);
            }
            catch (FormatException exp)
            {
                if (this.txtRESBUILDINGAREA.Text != "")
                {
                    MessageBox.Show("住宅建筑面积应为数值类型！", "楼盘信息编辑");
                    return false;
                }
            }

            try
            {
                pRow["doors"] = Double.Parse(this.txtDOORS.Text);
            }
            catch (FormatException exp)
            {
                if (this.txtDOORS.Text != "")
                {
                    MessageBox.Show("户数应为数值类型！", "楼盘信息编辑");
                    return false;
                }
            }

            try
            {
                pRow["housearearange1"] = Double.Parse(this.txtHOUSEAREARANGE1.Text);
            }
            catch (FormatException exp)
            {
                if (this.txtHOUSEAREARANGE1.Text != "")
                {
                    MessageBox.Show("户型面积范围应为数值类型！", "楼盘信息编辑");
                    return false;
                }
            }

            try
            {
                pRow["housearearange2"] = Double.Parse(this.txtHOUSEAREARANGE2.Text);
            }
            catch (FormatException exp)
            {
                if (this.txtHOUSEAREARANGE2.Text != "")
                {
                    MessageBox.Show("户数应为数值类型！", "楼盘信息编辑");
                    return false;
                }
            }
           
            if(this.dtpKPDATE.Checked==true)
			{
				pRow["kpdate"] = this.dtpKPDATE.Value.ToShortDateString();
			}
			else
			{
				pRow["kpdate"]=DBNull.Value;
			}

            if (this.dtpDCDATE.Checked == true)
            {
                pRow["dcdate"] = this.dtpDCDATE.Value.ToShortDateString();
            }
            else
            {
                pRow["dcdate"] = DBNull.Value;
            }

            //填写物业类型字段
			string strPropertyType="";
			for(int i = 0;i<=  this.lstPropertyType.Items.Count - 1;i++)
			{
				if(this.lstPropertyType.GetItemChecked(i))
				{
	                 
					strPropertyType = "1" + strPropertyType;
				}
				else
				{  
					strPropertyType = "0" + strPropertyType;
	             
				}
	        
			}
            pRow["propertytype"]= strPropertyType;

            if (this.m_FormType == FormType.NEW)
            {
                pDataSet.Tables["rsis_housebatch"].Columns.Remove("id");
                pDataSet.Tables["rsis_housebatch"].Rows.Add(pRow);

            }
            pDataAdp.Update(pDataSet, "rsis_housebatch");
            m_strHouseBatchName= this.txtHouseBatch.Text;
            return true;

          }

        /// <summary>
        /// 加载当前数据
        /// </summary>
        public void LoadData()
        {
            char[] delimiter={' '};
			
            //m_pAxMapControl.OnMouseDown+=new IMapControlEvents2_OnMouseDownEventHandler(m_pAxMapControl_OnMouseDown);

            if ((this.m_FormType == FormType.MOD) | (this.m_FormType == FormType.VIEW))
            {
                RSIS.DataBase.RealInfo pRealInfo = new RSIS.DataBase.RealInfo();

                //加载现有属性
                Npgsql.NpgsqlConnection pADOCon = new Npgsql.NpgsqlConnection(pRealInfo.GetADOConStr());
                string strQueryString = "Select * from rsis_housebatch where houseid='"+m_strHouseId+"'and batchname='"+m_strHouseBatchName+"'";
                Npgsql.NpgsqlDataAdapter pSqlDataAdp = new Npgsql.NpgsqlDataAdapter();
                pSqlDataAdp.SelectCommand = new Npgsql.NpgsqlCommand(strQueryString, pADOCon);
                Npgsql.NpgsqlCommandBuilder pSqlComBuilder = new Npgsql.NpgsqlCommandBuilder(pSqlDataAdp);

                System.Data.DataSet pDataSet = new DataSet();
                pSqlDataAdp.Fill(pDataSet, "rsis_housebatch");
                pDataSet.Tables["rsis_housebatch"].PrimaryKey = new DataColumn[] { pDataSet.Tables["rsis_housebatch"].Columns["ID"] };
                System.Data.DataRow pRow = pDataSet.Tables["rsis_housebatch"].Rows[0];
                                
                //列出tab内信息
                if (pRow["batchname"] != DBNull.Value)
                {
                    this.txtHouseBatch.Text = pRow["batchname"].ToString();
                }

                for (int i = 0;i<= pRow["PROPERTYTYPE"].ToString(). Length - 1;i++)
				{
					if (pRow["PROPERTYTYPE"].ToString()[pRow["PROPERTYTYPE"].ToString().Length- i-1] == '1')
					{

						this.lstPropertyType.SetItemChecked(i,true);

					}
				}

                if (pRow["kpdate"] == DBNull.Value)
                {
                    this.dtpKPDATE.Checked = false;
                }
                else
                {
                    this.dtpKPDATE.Checked = true;
                    this.dtpKPDATE.Value = DateTime.Parse(pRow["kpdate"].ToString().Split(delimiter)[0]);
                }

                if (pRow["dcdate"] == DBNull.Value)
                {
                    this.dtpDCDATE.Checked = false;
                }
                else
                {
                    this.dtpDCDATE.Checked = true;
                    this.dtpDCDATE.Value = DateTime.Parse(pRow["dcdate"].ToString().Split(delimiter)[0]);
                }

                if (pRow["buildingarea"] != DBNull.Value)
                {
                    this.txtBuildingarea.Text = pRow["buildingarea"].ToString();
                }

                if (pRow["buildingarea"] != DBNull.Value)
                {
                    this.txtBuildingarea.Text = pRow["buildingarea"].ToString();
                }

                if (pRow["resbuildingarea"] != DBNull.Value)
                {
                    this.txtRESBUILDINGAREA.Text = pRow["resbuildingarea"].ToString();
                }

                if (pRow["combuildingarea"] != DBNull.Value)
                {
                    this.txtCOMBUILDINGAREA.Text = pRow["combuildingarea"].ToString();
                }

                if (pRow["floors"] != DBNull.Value)
                {
                    this.txtFLOORS.Text = pRow["floors"].ToString();
                }

                if (pRow["doors"] != DBNull.Value)
                {
                    this.txtDOORS.Text = pRow["doors"].ToString();
                }

                if (pRow["elevator"] != DBNull.Value)
                {
                    this.txtELEVATOR.Text = pRow["elevator"].ToString();
                }

                if (pRow["payment"] != DBNull.Value)
                {
                    this.txtPAYMENT.Text = pRow["payment"].ToString();
                }

                if (pRow["housetyperange"] != DBNull.Value)
                {
                    this.txtHOUSETYPERANGE.Text = pRow["housetyperange"].ToString();
                }

                if (pRow["housearearange1"] != DBNull.Value)
                {
                    this.txtHOUSEAREARANGE1.Text = pRow["housearearange1"].ToString();
                }

                if (pRow["housearearange1"] != DBNull.Value)
                {
                    this.txtHOUSEAREARANGE1.Text = pRow["housearearange1"].ToString();
                }

                if (pRow["housearearange2"] != DBNull.Value)
                {
                    this.txtHOUSEAREARANGE2.Text = pRow["housearearange2"].ToString();
                }

                if (pRow["housetypetrait"] != DBNull.Value)
                {
                    this.txtHOUSETYPETRAIT.Text = pRow["housetypetrait"].ToString();
                }

                if (pRow["meo"] != DBNull.Value)
                {
                    this.txtMeo.Text = pRow["meo"].ToString();
                }


                ////添加主力户型信息
                //string strQueryString1 = "Select * from rsis_mainhousetype Where houseid='" + m_strHouseId + "' and batch='"+m_strHouseBatchName+"'";
                //m_pHouseMainTypeDataAdapter = new Npgsql.NpgsqlDataAdapter();
                //m_pHouseMainTypeDataAdapter.SelectCommand = new Npgsql.NpgsqlCommand(strQueryString1, pADOCon);
                //Npgsql.NpgsqlCommandBuilder pComBuilder = new Npgsql.NpgsqlCommandBuilder(m_pHouseMainTypeDataAdapter);
                //m_pHouseMainTypeTable = new DataTable();
                //m_pHouseMainTypeDataAdapter.Fill(m_pHouseMainTypeTable);
                ////pDataSet.Tables["HOUSE"].PrimaryKey=new DataColumn[] {pDataSet.Tables["HOUSE"].Columns["ID"]} ;
                //System.Windows.Forms.ListViewItem pLvwItem;
             
                // m_pHouseMainTypeTable.Columns.Remove("id");
                //foreach (System.Data.DataRow pRow1 in m_pHouseMainTypeTable.Rows)
                //{
                //    if (pRow1["housetype"] == DBNull.Value)
                //    {
                //        pLvwItem = this.lvwMainHouseType.Items.Add("");
                //    }
                //    else
                //    {
                //        pLvwItem = this.lvwMainHouseType.Items.Add(pRow1["housetype"].ToString());

                //    }

                //    //pLvwItem.Tag = pRow["ID"].ToString();

                //    if (pRow1["housetypetrait"] == DBNull.Value)
                //    {
                //        pLvwItem.SubItems.Add("");
                //    }
                //    else
                //    {
                //        pLvwItem.SubItems.Add(pRow1["housetypetrait"].ToString()); ;

                //    }

                //    if (pRow1["housearearange"] == DBNull.Value)
                //    {
                //        pLvwItem.SubItems.Add("");
                //    }
                //    else
                //    {
                //        pLvwItem.SubItems.Add(pRow1["housearearange"].ToString());

                //    }

                //    if (pRow1["housearearange2"] == DBNull.Value)
                //    {
                //        pLvwItem.SubItems.Add("");
                //    }
                //    else
                //    {
                //        pLvwItem.SubItems.Add(pRow1["housearearange2"].ToString()); ;

                //    }
                //    pLvwItem.Tag = pRow1;

                //    if (pRow1["batch"] == DBNull.Value)
                //    {
                //        pLvwItem.SubItems.Add("");
                //    }
                //    else
                //    {
                //        pLvwItem.SubItems.Add(pRow1["batch"].ToString()); ;

                //    }

                //}


            }
        }

        /// <summary>
        /// 删除期/批次信息
        /// </summary>
        public void Delete()
        {
                 RSIS.DataBase.RealInfo pRealInfo = new RSIS.DataBase.RealInfo();

                //加载现有属性
                Npgsql.NpgsqlConnection pCon = new Npgsql.NpgsqlConnection(pRealInfo.GetADOConStr());
                string strQueryString = "Select * from rsis_housebatch where houseid='" + m_strHouseId + "'and batchname='" + m_strHouseBatchName + "'";
                Npgsql.NpgsqlDataAdapter pDataAdp = new Npgsql.NpgsqlDataAdapter();
                pDataAdp.SelectCommand = new Npgsql.NpgsqlCommand(strQueryString, pCon);
                Npgsql.NpgsqlCommandBuilder pComBuilder = new Npgsql.NpgsqlCommandBuilder(pDataAdp);

                System.Data.DataSet pDataSet = new DataSet();
                pDataAdp.Fill(pDataSet, "rsis_housebatch");
                                                             
                pDataSet.Tables["rsis_housebatch"].Rows[0].Delete();
                pDataAdp.Update(pDataSet, "rsis_housebatch");
         }

        
        //private void btnadd_Click(object sender, EventArgs e)
        //{
        //    System.Windows.Forms.ListViewItem pLvwItem;

        //    MainHouseTypeTraitForm frmMainHouseTrait = new MainHouseTypeTraitForm();
        //    frmMainHouseTrait.m_FormType = FormType.NEW;
        //    frmMainHouseTrait.m_strHouseID = m_strHouseId;
        //    frmMainHouseTrait.cmbBatch.Text = m_strHouseBatchName;
        //    frmMainHouseTrait.MainHouseTypeTable = m_pHouseMainTypeTable;
          
        //    if (frmMainHouseTrait.ShowDialog() == DialogResult.OK)
        //    {
        //        this.lvwMainHouseType.Items.Clear();
        //        m_pHouseMainTypeTable = frmMainHouseTrait.MainHouseTypeTable;
        //        foreach (System.Data.DataRow pRow1 in m_pHouseMainTypeTable.Rows)
        //        {
        //            if (pRow1.RowState != DataRowState.Deleted)
        //            {
        //                if (pRow1["housetype"] == DBNull.Value)
        //                {
        //                    pLvwItem = this.lvwMainHouseType.Items.Add("");
        //                }
        //                else
        //                {
        //                    pLvwItem = this.lvwMainHouseType.Items.Add(pRow1["housetype"].ToString());

        //                }

        //                //pLvwItem.Tag = pRow["ID"].ToString();

        //                if (pRow1["housetypetrait"] == DBNull.Value)
        //                {
        //                    pLvwItem.SubItems.Add("");
        //                }
        //                else
        //                {
        //                    pLvwItem.SubItems.Add(pRow1["housetypetrait"].ToString()); ;

        //                }

        //                if (pRow1["housearearange"] == DBNull.Value)
        //                {
        //                    pLvwItem.SubItems.Add("");
        //                }
        //                else
        //                {
        //                    pLvwItem.SubItems.Add(pRow1["housearearange"].ToString());

        //                }

        //                if (pRow1["housearearange2"] == DBNull.Value)
        //                {
        //                    pLvwItem.SubItems.Add("");
        //                }
        //                else
        //                {
        //                    pLvwItem.SubItems.Add(pRow1["housearearange2"].ToString()); ;

        //                }
        //                pLvwItem.Tag = pRow1;
                                               
        //            }

        //        }
        //    }
        //}

        //private void btnmodify_Click(object sender, EventArgs e)
        //{

        //    if (this.lvwMainHouseType.SelectedItems.Count == 1)
        //    {
        //        System.Windows.Forms.ListViewItem pLvwItem;
        //        MainHouseTypeTraitForm frmMainHouseTrait = new MainHouseTypeTraitForm();
        //        frmMainHouseTrait.m_FormType = FormType.MOD;
        //        frmMainHouseTrait.m_strHouseID =m_strHouseId;
        //        frmMainHouseTrait.DataRow = (System.Data.DataRow)this.lvwMainHouseType.SelectedItems[0].Tag;
        //        frmMainHouseTrait.MainHouseTypeTable = m_pHouseMainTypeTable;
        //        //frmSaleInfo.ShowDialog();
        //        if (frmMainHouseTrait.ShowDialog() == DialogResult.OK)
        //        {
        //            this.lvwMainHouseType.Items.Clear();
        //            m_pHouseMainTypeTable = frmMainHouseTrait.MainHouseTypeTable;
        //            foreach (System.Data.DataRow pRow1 in m_pHouseMainTypeTable.Rows)
        //            {
        //                if (pRow1.RowState != DataRowState.Deleted)
        //                {
        //                    if (pRow1["housetype"] == DBNull.Value)
        //                    {
        //                        pLvwItem = this.lvwMainHouseType.Items.Add("");
        //                    }
        //                    else
        //                    {
        //                        pLvwItem = this.lvwMainHouseType.Items.Add(pRow1["housetype"].ToString());

        //                    }

        //                    //pLvwItem.Tag = pRow["ID"].ToString();

        //                    if (pRow1["housetypetrait"] == DBNull.Value)
        //                    {
        //                        pLvwItem.SubItems.Add("");
        //                    }
        //                    else
        //                    {
        //                        pLvwItem.SubItems.Add(pRow1["housetypetrait"].ToString()); ;

        //                    }

        //                    if (pRow1["housearearange"] == DBNull.Value)
        //                    {
        //                        pLvwItem.SubItems.Add("");
        //                    }
        //                    else
        //                    {
        //                        pLvwItem.SubItems.Add(pRow1["housearearange"].ToString());

        //                    }

        //                    if (pRow1["housearearange2"] == DBNull.Value)
        //                    {
        //                        pLvwItem.SubItems.Add("");
        //                    }
        //                    else
        //                    {
        //                        pLvwItem.SubItems.Add(pRow1["housearearange2"].ToString()); ;

        //                    }
        //                    pLvwItem.Tag = pRow1;

        //                    if (pRow1["batch"] == DBNull.Value)
        //                    {
        //                        pLvwItem.SubItems.Add("");
        //                    }
        //                    else
        //                    {
        //                        pLvwItem.SubItems.Add(pRow1["batch"].ToString()); ;

        //                    }
        //                }

        //            }
        //        }
        //    }
        //    else
        //    {
        //        MessageBox.Show("请先选中一条记录！");
        //        return;
        //    }

        //}

        //private void btndel_Click(object sender, EventArgs e)
        //{
        //    if (this.lvwMainHouseType.SelectedItems.Count == 1)
        //    {

        //        if (System.Windows.Forms.MessageBox.Show("是否删除当前主力房型信息？", "楼盘编辑工具", MessageBoxButtons.YesNo) == DialogResult.Yes)
        //        {

        //            RSIS.DataBase.RealInfo pRealInfo = new RSIS.DataBase.RealInfo();


        //            System.Data.DataRow pRow = (System.Data.DataRow)this.lvwMainHouseType.SelectedItems[0].Tag;
        //            pRow.Delete();
        //            this.lvwMainHouseType.SelectedItems[0].Remove();

        //        }



        //    }

        //    else
        //    {

        //        MessageBox.Show("请先选中一条记录！");
        //        return;

        //    }
        //}

        private void btnHOUSETYPETRAIT_Click(object sender, EventArgs e)
        {
            TextForm frmText = new TextForm();
            frmText.txtValue.Text = this.txtHOUSETYPETRAIT.Text;
            frmText.Text = this.lbHOUSETYPETRAIT.Text;
            if (this.m_FormType == FormType.VIEW)
            {
                frmText.btnOK.Enabled = false;
                frmText.btnOK.Visible = false;

            }

            if (frmText.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtHOUSETYPETRAIT.Text = frmText.txtValue.Text;
            }
        }

        public string BatchName
        {
            get
            {
                return m_strHouseBatchName;
            }
        }

        private void txtMeo_TextChanged(object sender, EventArgs e)
        {

        }


        private void btnMeo_Click(object sender, EventArgs e)
        {
            TextForm frmText = new TextForm();
            frmText.txtValue.Text = this.txtMeo.Text;
            frmText.Text = this.lbHOUSETYPETRAIT.Text;
            if (this.m_FormType == FormType.VIEW)
            {
                frmText.btnOK.Enabled = false;
                frmText.btnOK.Visible = false;

            }

            if (frmText.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtMeo.Text = frmText.txtValue.Text;
            }

        }

   
       



   
    }
}
